package com.cyjt.operation.uitools;

import android.util.Log;

/**
 * Created by mac on 15/4/10.
 */
public class HardwareInterfaceTools {
	private static final String SOF = "AA22";
	private static final String NUM = "0000";
	private static final String UK_DD00 = "DD00";
	private static final String UK_D100 = "D100";
	private static final String SIZE_0B = "0B";
	private static final String SIZE_03 = "03";

	private static final String OTHER = "000000";

	/**
	 * 提交静态部署基站绑定信息
	 * 
	 * @param nfc
	 * @param code
	 * @return
	 */
	public static String getSubmitForConBaseStationMessage(String nfc,
			String code) {
		// 计算CRC
		Log.v("demo", "要解析－－－－－－－－>  " + SOF + NUM + UK_DD00 + SIZE_0B + "0000"
				+ nfc + code + OTHER);
		String CRC = "0000"
				+ getCRC16Code(SOF + NUM + UK_DD00 + SIZE_0B + "0000" + nfc
						+ code + OTHER);
		CRC = CRC.substring(CRC.length() - 4, CRC.length());
		Log.v("demo", "要发送－－－－－－－－>  " + SOF + NUM + UK_DD00 + SIZE_0B + CRC
				+ nfc + code + OTHER);
		return SOF + NUM + UK_DD00 + SIZE_0B + CRC + nfc + code + OTHER;
	}
	
	/**
	 * 提交动态部署基站绑定信息
	 * @param nfc
	 * @param code
	 * @param position
	 * @return
	 */
	public static String getDynamicSubmitForConBaseStationMessage(String nfc,
			String code, String position) {
		// 计算CRC
		Log.v("demo", "要解析－－－－－－－－>  " + SOF + NUM + UK_DD00 + SIZE_0B + "0000"
				+ nfc + code + getPosition(position));
		String CRC = "0000"
				+ getCRC16Code(SOF + NUM + UK_DD00 + SIZE_0B + "0000" + nfc
						+ code + getPosition(position));
		CRC = CRC.substring(CRC.length() - 4, CRC.length());
		Log.v("demo", "要发送－－－－－－－－>  " + SOF + NUM + UK_DD00 + SIZE_0B + CRC
				+ nfc + code + getPosition(position));
		return SOF + NUM + UK_DD00 + SIZE_0B + CRC + nfc + code + position;
	}

	/**
	 * 返回向位
	 * 
	 * @param other
	 * @return
	 */
	public static String getPosition(String other) {
		String position = other.substring(0, 3).toString();
		if ("南北向".equals(position)) {
			return "000000";
		} else if ("东西向".equals(position)) {
			return "010000";
		}
		return "";
	}

	/**
	 * 返回部署基站，绑定成功
	 * 
	 * @param nfc
	 * @return
	 */
	public static String getSubmitForBindingFinishedResponseMessage(String nfc) {
		// 计算CRC
		Log.v("demo", "要解析－－－－－－－－>  " + SOF + NUM + UK_D100 + SIZE_03 + "0000"
				+ nfc);
		String CRC = "0000"
				+ getCRC16Code(SOF + NUM + UK_D100 + SIZE_03 + "0000" + nfc);
		CRC = CRC.substring(CRC.length() - 4, CRC.length());
		Log.v("demo", "要发送－－－－－－－－>  " + SOF + NUM + UK_D100 + SIZE_03 + CRC
				+ nfc);
		return SOF + NUM + UK_D100 + SIZE_03 + CRC + nfc;
	}

	/**
	 * 解析硬件接口发送出来的数据指令
	 * 
	 * @param message
	 * @return
	 */
	public static int HandlerResponseMessage(String message) {
		message = message.replace(" ", "");
		Log.v("demo", "处理字段：" + message);
		String code = message.subSequence(8, 12).toString();
		Log.v("demo", "指令码：" + code);
		if (code.equals(UK_DD00))
			return 0;
		if (code.equals(UK_D100))
			return 1;
		return -1;
	}

	/**
	 * 解析硬件接口发送出来的数据指令
	 * 
	 * @param message
	 * @return
	 */
	public static String HandlerResponseMessageContext(String message) {
		message = message.replace(" ", "");
		String code = message.subSequence(message.length() - 6,
				message.length()).toString();
		return code;
	}

	/**
	 * 计算CRC
	 * 
	 * @param msg
	 * @return
	 */
	private static String getCRC16Code(String msg) {
		int[] font = new int[msg.length() / 2];
		for (int i = 0; i < msg.length() / 2; i++) {
			Log.v("demo", "要解析－－－－－－－－>  " + msg.substring(2 * i, 2 * i + 2));
			font[i] = CRC16.Str2Hex16(msg.substring(2 * i, 2 * i + 2));
		}
		return Integer.toHexString(CRC16.CalcCRC2(font, font.length));
	}
}
